import 'package:flutter/material.dart';
import 'package:jd_shop/utils/ToastMsg.dart';
import 'package:provider/provider.dart';

import '../../provider/AppProvider.dart';
import 'CartPage.dart';
import 'ClassPage.dart';
import 'HomePage.dart';
import 'MyPage.dart';

class TabPage extends StatefulWidget {
  int currentIndex = 0;

  TabPage({super.key, this.currentIndex = 0});

  @override
  State<StatefulWidget> createState() {
    return _TabPageState();
  }
}

class _TabPageState extends State<TabPage> {
  final PageController _pageController = PageController();
  var _currentIndex = 0;
  final List<Widget> _pageList = [
    const HomePage(),
    const ClassPage(),
    const CartPage(),
    const MyPage(),
  ];

  @override
  void initState() {
    super.initState();
    if (widget.currentIndex != _currentIndex) {
      _currentIndex = widget.currentIndex;
      jumpToPage(_currentIndex);
    }
  }

  void jumpToPage(int index) {
    //等待100 毫秒 后跳转
    Future.delayed(const Duration(milliseconds: 50)).then((value) {
      setState(() {
        _currentIndex = index;
        _pageController.jumpToPage(index);
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        //禁止左右滑动 翻页
        physics: const NeverScrollableScrollPhysics(),
        controller: _pageController,
        children: _pageList,
        onPageChanged: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        fixedColor: Colors.blue,
        //设置选中字体大小  和 未选中字体大小
        selectedFontSize: 14,
        unselectedFontSize: 14,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
            _pageController.jumpToPage(index);
          });
        },
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: "首页",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.category),
            label: "分类",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.account_circle_rounded),
            label: "购物车",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: "我的",
          ),
        ],
      ),
    );
  }
}
